<template>
	<view class="content">
		<view class="" v-if="!showmima">
			<view class="" style="padding: 20rpx 0;">
				<u-search placeholder="请输入编号进行搜索" :showAction="true" actionText="搜索" v-model="keyword"
					@search="getIdData"></u-search>
			</view>
			<view class="" style="height: 78vh; overflow-y: auto;">
				<view class="" v-for="item in datasss">
					<!-- 编号:
					<u--input placeholder="编号" border="surround" v-model="item.name"></u--input>
					链接:
					<u--input placeholder="请输入需要修改的链接" border="surround" v-model="item.lianjie"></u--input> -->
					<u--form labelPosition="left" :model="admin1" ref="uForm">
						<u-form-item label="编号:" prop="item.name" borderBottom ref="item1">
							<u--input v-model="item.name"></u--input>
						</u-form-item>
						<u-form-item label="链接:" prop="item.lianjie" borderBottom ref="item1">
							<u--input v-model="item.lianjie"></u--input>
						</u-form-item>
					</u--form>
					<view style="padding: 30rpx; 0; display: flex; justify-content: space-between;">
						<u-button style="width: 31%;" type="primary" text="更新" @click="handlename(item)"></u-button>
						<u-button style="width: 31%;" type="primary" text="删除"
							@click="handlename_delete(item)"></u-button>
						<u-button style="width: 31%;" type="primary" text="复制链接" @click="save(item)"></u-button>
					</view>
				</view>
			</view>
			<view class="content_footer"
				style="display: flex;justify-content: space-between;align-items: center;height: 130rpx;">
				<u-button style="width: 45%;" type="primary" text="新增" @click="handlename_add"></u-button>
				<u-button style="width: 45%;" type="primary" text="小程序维护" @click="handlename_add_xiao"></u-button>
			</view>
		</view>
		<u-popup :show="show" @close="show = false" :round="10" mode="center">
			<view style="padding: 40rpx;">
				<u--form labelPosition="left" :model="admin1" ref="uForm">
					<u-form-item label="编号:" prop="formdata.name" borderBottom ref="item1">
						<u--input v-model="formdata.name"></u--input>
					</u-form-item>
					<u-form-item label="链接:" prop="formdata.lianjie" borderBottom ref="item1">
						<u--input v-model="formdata.lianjie"></u--input>
					</u-form-item>
				</u--form>
				<view style="padding: 30rpx; 0">
					<u-button type="primary" text="新增" @click="handlename_adds"></u-button>
				</view>
			</view>
		</u-popup>
		<u-popup :show="show1" @close="show1 = false" :round="10" mode="center">
			<view style=" padding: 40rpx;">
				需要更换的小程序appid:
				<u--input placeholder="需要更换的小程序appid" border="surround" v-model="formdata.appid"></u--input>
				<view style="padding: 15rpx; 0">
					<u-button type="primary" text="更换" @click="handlename_adds_xiao"></u-button>
				</view>
			</view>
		</u-popup>
		<u-popup :show="showmima" @close="showmima = false" :round="10" mode="center">
			<view style="padding: 40rpx;">
				<u--form labelPosition="left" :model="admin1" ref="uForm">
					<u-form-item label="密码:" prop="admin" borderBottom ref="item1">
						<u--input v-model="admin"></u--input>
					</u-form-item>
				</u--form>
				<u-button type="primary" text="验证" @click="yanzheng"></u-button>
			</view>
		</u-popup>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formdata: {
					appid: '',
					lianjie: '',
					apppath: '',
					name: ''
				},
				admin1: {},
				admin: '',
				show1: false,
				keyword: '',
				datasname: {},
				datasss: [],
				show: false,
				id: "",
				datasssNew: '',
				showmima:true
			}
		},
		onLoad() {
			if (!this.showmima) {
				this.showmima = true
			}
			// this.SaveData('', 'get_lianjie')
		},
		methods: {
			handlename_add() {
				this.show = true
			},
			yanzheng() {
				if (this.admin == "游戏岛屿") {
					this.showmima = false
					this.SaveData('', 'get_lianjie')
				} else {
					this.showmima = false
					uni.showToast({
						title: '密码错误！获取数据失败',
						icon: 'error', //success 等
					});
				}
			},
			handlename_add_xiao() {
				uniCloud.callFunction({
					name: 'uni-id-cf',
					data: {
						action: 'get',
						_id: '6608e3a6a7c432936bad3f6e',
						data: {

						}
					},
					success: res => {
						console.log(res.result)
						this.formdata.appid = res.result.data[0].appid
						this.show1 = true
					},
					fail: err => {
						console.error(err)
					}
				})
			},
			getIdData() {
				this.datasss = []
				if (this.keyword) {
					this.datasssNew.forEach(item => {
						if (item.name == this.keyword) {
							this.datasss.push(item)
						}
					})
				} else {
					this.datasss = this.datasssNew
				}
				console.log(this.datasss)
			},
			handlename_adds_xiao() {
				uniCloud.callFunction({
					name: 'uni-id-cf',
					data: {
						action: 'update',
						_id: '6608e3a6a7c432936bad3f6e',
						data: {
							appid: this.formdata.appid
						}
					},
					success: res => {
						console.log(res.result)
						this.show1 = false
						uni.showModal({
							title: '提示',
							content: '更新成功',
							success: function(res) {
								if (res.confirm) {} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					},
					fail: err => {
						console.error(err)
					}
				})
			},
			handlename_delete(data) {
				let me = this
				uni.showModal({
					title: '警告',
					content: '确认删除',
					success: function(res) {
						if (res.confirm) {
							uniCloud.callFunction({
								name: 'uni-id-cf',
								data: {
									action: 'delete_lianjie',
									_id: data._id,
									data: {}
								},
								success: res => {
									console.log(res.result)
									me.SaveData('', 'get_lianjie')
								},
								fail: err => {
									console.error(err)
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			handlename_adds() {
				let me = this
				uniCloud.callFunction({
					name: 'uni-id-cf',
					data: {
						action: "add_lianjie",
						data: {
							...this.formdata
						}
					},
					success: res => {
						console.log(res.result)
						this.id = res.result.id
						this.show = false
						uni.showModal({
							title: '提示',
							content: '新增成功',
							success: function(res) {
								if (res.confirm) {
									me.SaveData('', 'get_lianjie')
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					},
					fail: err => {
						console.error(err)
					}
				})
			},
			save(item) {
				uni.setClipboardData({
					data: 'https://static-mp-15664855-c6b0-43bb-ab42-9bad6993ccd7.next.bspapp.com?_id=' + item._id,
					success: function() {
						uni.showToast({
							title: '复制成功',
						});
					},
					fail: function() {
						uni.showToast({
							title: '复制失败',
						});
					}
				});
			},
			SaveData(data1, name) {
				uniCloud.callFunction({
					name: 'uni-id-cf',
					data: {
						action: name,
						_id: this.formdata.id,
						data: {
							...data1
						}
					},
					success: res => {
						console.log(res.result)
						this.datasss = res.result.data
						this.datasssNew = JSON.parse(JSON.stringify(this.datasss))
						// this.formdata.appid = res.result.data[0].appid
						// this.formdata.lianjie = res.result.data[0].lianjie
						// this.formdata.id = res.result.data[0]._id
					},
					fail: err => {
						console.error(err)
					}
				})
			},
			handlename(item) {
				uniCloud.callFunction({
					name: 'uni-id-cf',
					data: {
						action: 'update_lianjie',
						_id: item._id,
						data: {
							name: item.name,
							lianjie: item.lianjie,
							apppath: item.apppath
						}
					},
					success: res => {
						uni.showModal({
							title: '提示',
							content: '更新成功',
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					},
					fail: err => {
						console.error(err)
					}
				})


			}
		}
	}
</script>

<style>
	.content {
		box-sizing: border-box;
		padding: 30rpx;
	}
</style>